package com.guocheng.modules.terminals.service;

import com.guocheng.modules.terminals.mapper.TerminalsMapper;
import com.guocheng.modules.terminals.model.Terminals;
import com.guocheng.modules.terminals.utils.TerminalsUtils;
import com.guocheng.utils.TerminalsFiledUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;

import java.util.LinkedHashMap;
import java.util.List;

@Service
public class TerminalService {

    @Autowired
    private TerminalsMapper terminalsMapper;

    /**
     * 查询终端连接信息
     * @return
     */
    public Terminals selectTerminals(){
        return TerminalsUtils.terminals;
    }

    /**
     * 更改设备连接信息
     * @param terminals 要更改的信息
     * @return
     */
    @Transactional
    public int updateTerminals(Terminals terminals){
        //更改登录信息
        int t = terminalsMapper.updateTerminals(terminals);
        TerminalsUtils.terminals=terminals;
        //修改数据库
        return t;
    }

    /**
     * 登录测试
     * @return
     */
    public boolean login(Terminals terminals) {
        return TerminalsUtils.login(terminals);
    }

    /**
     * 发送get请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public ResponseEntity<LinkedHashMap> get(String url, LinkedMultiValueMap<String, Object> body) {
        //get请求拼接参数
        url = TerminalsUtils.bodyToGet(url, body);
        //获取请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        return TerminalsUtils.request(url, HttpMethod.GET, httpEntity);
    }

    /**
     * 发送get请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public <T> List<T> get(String url, LinkedMultiValueMap<String, Object> body,Class<T> c) {
        //get请求拼接参数
        url = TerminalsUtils.bodyToGet(url, body);
        //获取请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        ResponseEntity<LinkedHashMap> request = TerminalsUtils.request(url, HttpMethod.GET, httpEntity);
        //转换数据
        return TerminalsFiledUtils.to((List)request.getBody().get("data"), c);
    }


    /**
     * 发送post请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public static ResponseEntity<LinkedHashMap> post(String url, LinkedMultiValueMap<String, Object> body) {
        //请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        return TerminalsUtils.request(url, HttpMethod.POST, httpEntity);
    }

    /**
     * 发送post请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public static <T> List<T> post(String url, LinkedMultiValueMap<String, Object> body,Class<T> c) {
        //请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        ResponseEntity<LinkedHashMap> request = TerminalsUtils.request(url, HttpMethod.POST, httpEntity);
        //转换数据
        return TerminalsFiledUtils.to((List)request.getBody().get("data"),c);
    }

    /**
     * 发送put请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public static ResponseEntity<LinkedHashMap> put(String url, LinkedMultiValueMap<String, Object> body) {
        //请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        return TerminalsUtils.request(url, HttpMethod.PUT, httpEntity);
    }

    /**
     * 发送put请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public static <T> List<T> put(String url, LinkedMultiValueMap<String, Object> body,Class<T> c) {
        //请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        ResponseEntity<LinkedHashMap> request = TerminalsUtils.request(url, HttpMethod.PUT, httpEntity);
        //转换数据
        return TerminalsFiledUtils.to((List)request.getBody().get("data"), c);
    }

    /**
     * 发送delete请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public static ResponseEntity<LinkedHashMap> delete(String url, LinkedMultiValueMap<String, Object> body) {
        //请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        return TerminalsUtils.request(url, HttpMethod.DELETE, httpEntity);
    }

    /**
     * 发送delete请求
     *
     * @param url  请求地址
     * @param body 请求参数
     * @return
     */
    public static <T> List<T> delete(String url, LinkedMultiValueMap<String, Object> body,Class<T> c) {
        //请求体
        HttpEntity httpEntity = TerminalsUtils.getHttpEntity(body);
        //发送请求
        ResponseEntity<LinkedHashMap> request = TerminalsUtils.request(url, HttpMethod.DELETE, httpEntity);
        //转换数据
        return TerminalsFiledUtils.to((List)request.getBody().get("data"), c);
    }

}
